package com.code.wflow.controller;

import com.code.wflow.common.RequestContextHolder;
import com.code.wflow.constants.ResultCode;
import com.code.wflow.constants.WorkflowStatus;
import com.code.wflow.model.FormItem;
import com.code.wflow.model.Workflow;
import com.code.wflow.model.WorkflowNode;
import com.code.wflow.service.FormItemService;
import com.code.wflow.service.WorkflowNodeService;
import com.code.wflow.service.WorkflowService;
import com.code.wflow.utils.BeanCopierUtils;
import com.code.wflow.utils.FormItemUtils;
import com.code.wflow.utils.WorkflowUtils;
import com.code.wflow.wrapper.Result;
import com.code.wflow.wrapper.request.AddWorkflowRequest;
import com.code.wflow.wrapper.request.QueryWorkflowRequest;
import com.code.wflow.wrapper.request.WorkflowIdRequest;
import com.code.wflow.wrapper.response.WorkflowDetailVO;
import com.code.wflow.wrapper.response.WorkflowVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 流程相关
 *
 * @author ggh
 * @date 2022/10/26
 */
@RequestMapping(value = "/workflow")
@RestController
public class WorkflowController {

    @Autowired
    private WorkflowService workflowService;

    @Autowired
    private WorkflowNodeService workflowNodeService;

    @Autowired
    private FormItemService formItemService;

    /**
     * 新增流程
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/addWorkflow")
    public Result<String> addWorkflow(@RequestBody AddWorkflowRequest request) {
        if (StringUtils.isEmpty(request.getFormName()) || StringUtils.isEmpty(request.getProcess()) || StringUtils.isEmpty(request.getFormItems())) {
            return Result.failure(ResultCode.PARAM_LACK);
        }
        try {
            Workflow workflow = new Workflow();
            workflow.setFormId(request.getFormId());
            workflow.setLogo(request.getLogo());
            workflow.setFormName(request.getFormName());
            workflow.setSettings(request.getSettings());
            workflow.setCreateUserId(RequestContextHolder.getContext().getUserId());

            // 流程创建后，默认启用
            if (workflow.getStatus() == null) {
                workflow.setStatus(WorkflowStatus.ENABLE.getVal());
            }

            // 解析form表单信息和流程节点
            List<FormItem> formItemList = FormItemUtils.parseFormItems(request.getFormItems());
            List<WorkflowNode> workflowNodeList = WorkflowUtils.parseWorkflowDef(request.getProcess());

            if (workflowService.insert(workflow, workflowNodeList, formItemList)) {
                return Result.ok("新增流程成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.failure(ResultCode.WORKFLOW_ADD_ERROR);
    }

    /**
     * 查询流程
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryWorkflow")
    public Result<List<WorkflowVO>> queryWorkflow(@RequestBody QueryWorkflowRequest request) {
        try {
            List<Workflow> workflowList = workflowService.queryByConditions(request.getKeyword(), request.getStatus());
            List<WorkflowVO> workflowVOList = BeanCopierUtils.copyList(workflowList, WorkflowVO.class);

            return Result.ok(workflowVOList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.failure(ResultCode.WORKFLOW_QUERY_ERROR);
    }

    /**
     * 查看流程详情
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryWorkflowDetail")
    public Result<WorkflowDetailVO> queryWorkflowDetail(@RequestBody WorkflowIdRequest request) {
        if (request == null || request.getWorkflowId() == null) {
            return Result.failure(ResultCode.PARAM_LACK);
        }
        try {
            Workflow workflow = workflowService.queryById(request.getWorkflowId());
            if (workflow == null) {
                return Result.failure(ResultCode.WORKFLOW_NOT_EXIST);
            }

            List<WorkflowNode> workflowNodeList = workflowNodeService.queryByWorkflowId(request.getWorkflowId());
            List<FormItem> formItemList = formItemService.queryByWorkflowId(request.getWorkflowId());

            WorkflowDetailVO workflowDetailVO = BeanCopierUtils.copy(workflow, WorkflowDetailVO.class);
            workflowDetailVO.setProcess(WorkflowUtils.buildWorkflowDef(workflowNodeList));
            workflowDetailVO.setFormItems(FormItemUtils.buildFormItemDef(formItemList));

            return Result.ok(workflowDetailVO);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.failure(ResultCode.WORKFLOW_QUERY_ERROR);
    }
}
